/**
 * 
 */
package com.gopawpaw.android.demo.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gopawpaw.android.demo.R;

/**
 * @author Jason
 * @since: 2011-6-8
 */
public class AsyncTaskActivity extends Activity {
	private static final String TAG = "AsyncTaskActivity";
	
	private TextView message = null;
	
	
	/**
	 * 
	 */
	public AsyncTaskActivity() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_asynctask_activity);
		
		message = (TextView)findViewById(R.id.textview_content);
		
		
		
		
		PageTask task = new PageTask();  
		task.execute("");
	}

	
	// 设置三种类型参数分别为String,Integer,String  
    class PageTask extends AsyncTask<String, Integer, String> {  
    	private ProgressDialog progressDialog = null;
        // 可变长的输入参数，与AsyncTask.exucute()对应  
        @Override  
        protected String doInBackground(String... params) {
        	
        	int count = 0;
        	while(count < 100){
        		publishProgress(count);
        		// 为了在模拟器中清楚地看到进度，让线程休眠100ms  
        		try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				count++;
        	}
        	
            return "ok";
        }  
        @Override  
        protected void onCancelled() {  
            super.onCancelled();
        }  
        @Override  
        protected void onPostExecute(String result) {  
            // 返回HTML页面的内容
            message.setText(result);
            
            if(progressDialog.isShowing()){
				progressDialog.dismiss();
			}
        }  
        @Override  
        protected void onPreExecute() {  
            // 任务启动，可以在这里显示一个对话框，这里简单处理  
            
            if(progressDialog==null){
				progressDialog=new ProgressDialog(AsyncTaskActivity.this);
				progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				
				progressDialog.setMessage("parsing...");
			}
			if(!progressDialog.isShowing()){
				progressDialog.setCancelable(false);
				progressDialog.show();
			}
        }  
        @Override  
        protected void onProgressUpdate(Integer... values) {  
            // 更新进度  
            message.setText(""+values[0]);
            progressDialog.setProgress(values[0]);
        }  
    }
}
